package com.ltkj.shop.service.impl;

import java.util.List;

import com.ltkj.common.core.constant.CacheConstants;
import com.ltkj.common.core.constant.Constants;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.redis.service.RedisService;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.shop.constants.ShopConstants;
import com.ltkj.shop.domain.vo.ShopCartVo;
import com.ltkj.shop.mapper.ShopCartBizMapper;
import com.ltkj.system.api.RemoteFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.shop.mapper.ShopCartMapper;
import com.ltkj.shop.domain.ShopCart;
import com.ltkj.shop.service.IShopCartService;

/**
 * 文创购物车Service业务层处理
 * 
 * @author xupf
 * @date 2023-08-29
 */
@Slf4j
@Service
public class ShopCartServiceImpl implements IShopCartService 
{
    private static Long CART_MAX_NUM = 100L;
    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private ShopCartBizMapper shopCartBizMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 查询文创购物车
     * 
     * @param id 文创购物车主键
     * @return 文创购物车
     */
    @Override
    public ShopCart selectShopCartById(Long id)
    {
        return shopCartMapper.selectShopCartById(id);
    }

    /**
     * 查询文创购物车列表
     * 
     * @param shopCart 文创购物车
     * @return 文创购物车
     */
    @Override
    public List<ShopCart> selectShopCartList(ShopCart shopCart)
    {
        return shopCartMapper.selectShopCartList(shopCart);
    }

    /**
     * 移动端查询文创购物车列表
     *
     * @param shopCart 文创购物车
     * @return 文创购物车
     */
    @Override
    public List<ShopCartVo> selectList(ShopCart shopCart)
    {
        return shopCartBizMapper.selectList(shopCart);
    }

    /**
     * 新增文创购物车
     * 
     * @param shopCart 文创购物车
     * @return 结果
     */
    @Override
    public AjaxResult insertShopCart(ShopCart shopCart)
    {
        shopCart.setCreateTime(DateUtils.getNowDate());
        shopCart.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        shopCart.setUserId(SecurityUtils.getLoginUser().getUserid());

        Object cacheObj = redisService.getCacheObject(CacheConstants.SYS_CONFIG_KEY + ShopConstants.SHOP_CART_MAX_NUM);
        Long maxNum = CART_MAX_NUM;
        try {
            maxNum = Long.valueOf((String) cacheObj);
        } catch (Exception e) {
            log.warn("获取购物车限制数量失败！{}", cacheObj.toString());
        }
        if (null != maxNum) {
            CART_MAX_NUM = maxNum;
        }
        Long count = shopCartBizMapper.countByUserId(shopCart.getUserId());
        if (count < CART_MAX_NUM) {
            // 根据商品、规格查询购物车
            ShopCart queryCart = new ShopCart();
            queryCart.setUserId(shopCart.getUserId());
            queryCart.setGoodsId(shopCart.getGoodsId());
            queryCart.setType(shopCart.getType());
            List<ShopCart> shopCarts = shopCartMapper.selectShopCartList(queryCart);
            // 如果不存在相同商品、规格的信息，则新增，反之修改
            int res = 0;
            if (shopCarts.isEmpty()) {
                res = shopCartMapper.insertShopCart(shopCart);
            } else {
                ShopCart oCart = shopCarts.get(0);
                oCart.setGoodsNum(oCart.getGoodsNum() + 1L);
                res = updateShopCart(oCart);
            }
            if (res == 0) {
                return AjaxResult.error();
            }
        } else {
            return AjaxResult.warn("添加失败！购物车数量已达上限!");
        }
        return AjaxResult.success();
    }

    /**
     * 修改文创购物车
     * 
     * @param shopCart 文创购物车
     * @return 结果
     */
    @Override
    public int updateShopCart(ShopCart shopCart)
    {
        shopCart.setUpdateTime(DateUtils.getNowDate());
        shopCart.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return shopCartMapper.updateShopCart(shopCart);
    }

    /**
     * 批量删除文创购物车
     * 
     * @param ids 需要删除的文创购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopCartByIds(Long[] ids)
    {
        return shopCartMapper.deleteShopCartByIds(ids);
    }

    /**
     * 删除文创购物车信息
     * 
     * @param id 文创购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopCartById(Long id)
    {
        return shopCartMapper.deleteShopCartById(id);
    }

    /**
     * 统计当前用户购物车数量
     * @return
     */
    @Override
    public Long countByUser() {
        return shopCartBizMapper.countByUserId(SecurityUtils.getLoginUser().getUserid());
    }

    /**
     * 清空购物车
     *
     * @param userId
     * @param goodsIds
     * @param types
     * @return
     */
    @Override
    public AjaxResult deleteByUser(Long userId, Long[] goodsIds, String[] types) {
        try {
            shopCartBizMapper.deleteByUser(userId, goodsIds, types);
        } catch (Exception e) {
            log.error("购物车移除失败！{}", e.toString());
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
